package org.artifact.core.server.mirror;

import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.core.io.resource.UrlResource;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import org.artifact.core.cache.SimpleLocalCache;
import org.artifact.core.context.bytebuf.NettyByteBuffFactory;
import org.artifact.core.context.json.FastJsonFactory;
import org.artifact.core.plugin.ioc.IocPlugin;
import org.artifact.core.server.ServerConfig;
import org.artifact.core.server.ServerContext;
import org.artifact.core.server.ServerPlugins;
import org.yaml.snakeyaml.Yaml;

import java.net.URL;
import java.util.Map;

public class DefaultServerMirror extends ServerMirror{

	
	static final Log log = LogFactory.get(DefaultServerMirror.class);

	@Override
	public void config(ServerConfig config) {
		// 加载默认配置文件
		URL url = ResourceUtil.getResourceObj("default.yml").getUrl();
		UrlResource urlResource = new UrlResource(url);
		Map conf = new Yaml().loadAs(urlResource.getStream(), Map.class);
		config.merge(conf);
	}

	@Override
	public void context(ServerContext context) {

		context.setJsonFactory(new FastJsonFactory());

		context.setByteBuffFactory(new NettyByteBuffFactory());

		context.setAttribute(new SimpleLocalCache());
	}

	@Override
	public ServerPlugins plugin(ServerPlugins plugins) {
		// 1.装载IOC插件
		plugins.load(new IocPlugin());

		return plugins;
	}
}
